package br.database;


import java.util.ArrayList;
import java.util.List;

import br.com.util.Config;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DbConfig {
	public static final String EMP_ID = "id";
	public static final String EMP_NAME = "name";
	public static final String ID = "idCat";
	public static final String EMP_TRUE = "falseA";


	
	private DatabaseHelperCAT mDbHelper;
	private SQLiteDatabase mDb;

	private static final String DATABASE_NAME = "Configuracoes.db";
	private static final int DATABASE_VERSION = 1;

	private static final String CATEGORY_TABLE = "Configuracoes";

	private static final String CREATE_CATEGORY_TABLE = "create table "
			+ CATEGORY_TABLE + " (" + EMP_ID + " integer primary key autoincrement, " + EMP_NAME + " text not null unique," + ID + " integer,"+ EMP_TRUE + " text not null unique);";
 	
	private final Context mCtx;

	private static class DatabaseHelperCAT extends SQLiteOpenHelper {
		DatabaseHelperCAT(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		public void onCreate(SQLiteDatabase db) {
			db.execSQL(CREATE_CATEGORY_TABLE);
		}

		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("DROP TABLE IF EXISTS " + CATEGORY_TABLE);
			onCreate(db);
		}
	}
	
	public void Reset() {
		mDbHelper.onUpgrade(this.mDb, 1, 1);
	}

	public DbConfig(Context ctx) {
		mCtx = ctx;
		mDbHelper = new DatabaseHelperCAT(mCtx);
	}

	public DbConfig open() throws SQLException {
		mDb = mDbHelper.getWritableDatabase();
		return this;
	}
	
	public void close() throws SQLException {
		mDbHelper.close();
	}

	public void insertEmpDetails(Confi employee) {
		ContentValues cv = new ContentValues();
		cv.put(EMP_NAME, employee.getNum());
		cv.put(EMP_TRUE, employee.getTrueA());
		cv.put(ID, EMP_ID);
		mDb.insert(CATEGORY_TABLE, null, cv);
	}

	
	public List<Confi> recuperarTodos() throws SQLException{
        String queryReturnAll = "SELECT * FROM " + CATEGORY_TABLE;
        Cursor cursor = mDb.rawQuery(queryReturnAll, null);
        List<Confi> categorias = construirVeiculoPorCursor(cursor);
 
        return categorias;
    }
	
	private List<Confi> construirVeiculoPorCursor(Cursor cursor) throws SQLException{
        List<Confi> categorias = new ArrayList<Confi>();
        if(cursor == null)
            return categorias;
        try {
 
            if (cursor.moveToFirst()) {
            	
                do {
                	// Log.i("xxx", "Pegando quemmlml.mlmlkmlkm:");
                	int indexMarca = cursor.getColumnIndex(EMP_NAME);
                    int indexModelo = cursor.getColumnIndex(EMP_ID);
                    int indexT = cursor.getColumnIndex(EMP_TRUE);
                    
                    int id = cursor.getInt(indexModelo);
                    String name = cursor.getString(indexMarca);
                    String t = cursor.getString(indexT);
 
                    Confi cat = new Confi(name,id,t);
 
                    categorias.add(cat);
 
                } while (cursor.moveToNext());
            }
             
        } finally {
            cursor.close();
        }
        return categorias;
    }
	
	 public void excluir(int id){
		 
			 Log.i("fsd", "QTD botao : ");
			    mDb = mDbHelper.getWritableDatabase();
			    int rows = mDb.delete("Configuracoes", "id = ?", new String[]{ String.valueOf(id) });
		 
		  //  return rows; // qtde. de linhas afetadas
	 }
	 
	 public Confi recuperarPorID(int id) {
	        String queryOne = "SELECT * FROM " + CATEGORY_TABLE + " where " + EMP_ID + " = " + id;
	        List<Confi> result = recuperarPorQuery(queryOne);
	        if(result.isEmpty()) {
	            return new Confi();
	        } else {
	            return result.get(0);  
	        }
	    }
		
		public List<Confi> recuperarPorQuery(String query) {
			 
	        Cursor cursor = mDb.rawQuery(query, null);
	 
	        List<Confi> result = new ArrayList<Confi>();
	        if (cursor.moveToFirst()) {
	            do {
	                ContentValues contentValues = new ContentValues();
	                DatabaseUtils.cursorRowToContentValues(cursor, contentValues);
	                Confi t = contentValuesParaEntidade(contentValues);
	                result.add(t);
	            } while (cursor.moveToNext());
	        }
	        return result;
	    }
		
		public Confi contentValuesParaEntidade(ContentValues contentValues) {
			Confi c = new Confi();
	        c.setId(contentValues.getAsInteger(EMP_ID));
	        c.setNum(contentValues.getAsString(EMP_NAME));
	        c.setTrueA(contentValues.getAsString(EMP_TRUE));

	        return c;
	    }
		
		public Confi recuperarPorNome(String nome) {
	        String queryOne = "SELECT * FROM " + CATEGORY_TABLE + " where name = '" + nome + "'" ;
	        List<Confi> result = recuperarPorQuery(queryOne);
	        if(result.isEmpty()) {
	            return new Confi();
	        } else {
	            return result.get(0);  
	        }
	    }
		
		public Integer getEmpId(String nome) {
			String queryOne = "SELECT * FROM " + CATEGORY_TABLE + " where name = '" + nome + "'" ;
	        List<Confi> result = recuperarPorQuery(queryOne);
	        if(result.isEmpty()) {
	            return -1;
	        } else {
	            return result.get(0).getId();  
	        }
		}
		
		public void update(Confi c) {
			ContentValues cv = new ContentValues();
			cv.put(EMP_NAME, c.getNum());
			cv.put(EMP_TRUE, c.getTrueA());

			Log.i("khkhkljlnlk", "lkjlkjljlkj:   " + c.getNum());

			// update
			Log.i("ojlkl", "CarDAO > UPDATE : " + c.getId());
			mDb.update("Configuracoes", cv,"id=" + 1, null);
			Log.i("khkhkljlnlk", "lkjlkjljlkj: " + cv);

		//	mDbHelper.getDataBase().update("Botoes ", cv, " col_id = ? ",new String[]{ String.valueOf(b.getId()) });
		}
}
